{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problems\n",
    "1. torchscript из модели detectron2, что можно конвертировать трейсингом (backbone — ok, RPN — failed). Возможно здесь стоит использовать еще scripting. \n",
    "2. tensorpack переводит detectron2 модель (не densepose) в статический граф TF, из него можно получить TFLite. Но это тоже вопрос для изучения\n",
    "3. fasterrcnn/maskrccn из torchvision можно преобразовать в ONNX (буквально со вчера). Но используя backend caffe2 не удалось исполнить эти модели, у них opset ONNX 10 и 11, а в caffe — 9.\n",
    "4. На backend tf так же не удается запустить модели из ONNX"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Detecron2 -> TorchScript\n",
    "Needed pytorch>=1.3, detectron2\n",
    "In next section it is needed to install pytorch-nightly, so install it here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('./DensePose/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from detectron2.config import get_cfg\n",
    "from detectron2.modeling import build_model\n",
    "from detectron2.checkpoint import DetectionCheckpointer\n",
    "from detectron2.data import DatasetMapper\n",
    "\n",
    "from DensePose.densepose import add_densepose_config\n",
    "\n",
    "\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Loading DensePose model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Config './DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml' has no VERSION. Assuming it to be compatible with latest v2.\n"
     ]
    }
   ],
   "source": [
    "config_file = './DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml'\n",
    "model_path = './DensePose/output/model_final.pth'\n",
    "opts = ['MODEL.WEIGHTS', model_path]\n",
    "cfg = get_cfg()\n",
    "add_densepose_config(cfg)\n",
    "cfg.merge_from_file(config_file)\n",
    "cfg.merge_from_list(opts)\n",
    "cfg.freeze()\n",
    "\n",
    "model = build_model(cfg)\n",
    "model = model.eval()\n",
    "\n",
    "DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(\n",
    "            cfg.MODEL.WEIGHTS, resume=False\n",
    ")\n",
    "mapper = DatasetMapper(cfg, is_train=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 3, 768, 1344])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inpput = [mapper({'file_name': './DensePose/2.jpg'})]\n",
    "images = model.preprocess_image(inpput)\n",
    "images.tensor.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create backbone wrapper and do tracing --- ok"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py:1030: TracerWarning: Output nr 1. of the traced function does not match the corresponding output of the Python function. Detailed error:\n",
      "Not within tolerance rtol=1e-05 atol=1e-05 at input[0, 58, 32, 244] (-0.13124431669712067 vs. -0.131231427192688) and 17 other locations (0.00%)\n",
      "  check_tolerance, _force_outplace, True, _module_class)\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py:1030: TracerWarning: Output nr 2. of the traced function does not match the corresponding output of the Python function. Detailed error:\n",
      "Not within tolerance rtol=1e-05 atol=1e-05 at input[0, 180, 83, 91] (0.03434910625219345 vs. 0.03433563560247421) and 16 other locations (0.00%)\n",
      "  check_tolerance, _force_outplace, True, _module_class)\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py:1030: TracerWarning: Output nr 3. of the traced function does not match the corresponding output of the Python function. Detailed error:\n",
      "Not within tolerance rtol=1e-05 atol=1e-05 at input[0, 207, 2, 3] (-0.0014769919216632843 vs. -0.001464206725358963) and 15 other locations (0.00%)\n",
      "  check_tolerance, _force_outplace, True, _module_class)\n"
     ]
    }
   ],
   "source": [
    "class backbone_wrapper(nn.Module):\n",
    "    def __init__(self, backbone):\n",
    "        super().__init__()\n",
    "        self.backbone = backbone\n",
    "    def forward(self, x):\n",
    "        fp = self.backbone(x)\n",
    "        return tuple(fp.values())\n",
    "    \n",
    "backbone_example = images\n",
    "backbone = model._modules['backbone'].eval()\n",
    "backbone_wrap = backbone_wrapper(backbone)\n",
    "traced_backbone = torch.jit.trace(backbone_wrap, backbone_example.tensor.to('cuda:0'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create RPN wrapper and do tracing --- failed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import division\n",
    "from typing import Any, List, Sequence, Tuple, Union\n",
    "\n",
    "class ImageList(object):\n",
    "    def __init__(self, tensor: torch.Tensor, image_sizes: List[Tuple[int, int]]):\n",
    "        \"\"\"\n",
    "        Arguments:\n",
    "            tensor (Tensor): of shape (N, H, W) or (N, C_1, ..., C_K, H, W) where K >= 1\n",
    "            image_sizes (list[tuple[int, int]]): Each tuple is (h, w).\n",
    "        \"\"\"\n",
    "        self.tensor = tensor\n",
    "        self.image_sizes = image_sizes\n",
    "\n",
    "    def __len__(self) -> int:\n",
    "        return len(self.image_sizes)\n",
    "    \n",
    "    @staticmethod\n",
    "    def from_tensors(\n",
    "        tensors: Sequence[torch.Tensor], size_divisibility: int = 0, pad_value: float = 0.0\n",
    "    ) -> \"ImageList\":\n",
    "        \"\"\"\n",
    "        Args:\n",
    "            tensors: a tuple or list of `torch.Tensors`, each of shape (Hi, Wi) or\n",
    "                (C_1, ..., C_K, Hi, Wi) where K >= 1. The Tensors will be padded with `pad_value`\n",
    "                so that they will have the same shape.\n",
    "            size_divisibility (int): If `size_divisibility > 0`, also adds padding to ensure\n",
    "                the common height and width is divisible by `size_divisibility`\n",
    "            pad_value (float): value to pad\n",
    "\n",
    "        Returns:\n",
    "            an `ImageList`.\n",
    "        \"\"\"\n",
    "        assert len(tensors) > 0\n",
    "        assert isinstance(tensors, (tuple, list))\n",
    "        for t in tensors:\n",
    "            assert isinstance(t, torch.Tensor), type(t)\n",
    "            assert t.shape[1:-2] == tensors[0].shape[1:-2], t.shape\n",
    "        # per dimension maximum (H, W) or (C_1, ..., C_K, H, W) where K >= 1 among all tensors\n",
    "        max_size = tuple(max(s) for s in zip(*[img.shape for img in tensors]))\n",
    "\n",
    "        if size_divisibility > 0:\n",
    "            import math\n",
    "\n",
    "            stride = size_divisibility\n",
    "            max_size = list(max_size)  # type: ignore\n",
    "            max_size[-2] = int(math.ceil(max_size[-2] / stride) * stride)  # type: ignore\n",
    "            max_size[-1] = int(math.ceil(max_size[-1] / stride) * stride)  # type: ignore\n",
    "            max_size = tuple(max_size)\n",
    "\n",
    "        image_sizes = [im.shape[-2:] for im in tensors]\n",
    "\n",
    "        if len(tensors) == 1:\n",
    "            # This seems slightly (2%) faster.\n",
    "            # TODO: check whether it's faster for multiple images as well\n",
    "            image_size = image_sizes[0]\n",
    "            padded = F.pad(\n",
    "                tensors[0],\n",
    "                [0, max_size[-1] - image_size[1], 0, max_size[-2] - image_size[0]],\n",
    "                value=pad_value,\n",
    "            )\n",
    "            batched_imgs = padded.unsqueeze_(0)\n",
    "        else:\n",
    "            batch_shape = (len(tensors),) + max_size\n",
    "            batched_imgs = tensors[0].new_full(batch_shape, pad_value)\n",
    "            for img, pad_img in zip(tensors, batched_imgs):\n",
    "                pad_img[..., : img.shape[-2], : img.shape[-1]].copy_(img)\n",
    "\n",
    "        return ImageList(batched_imgs.contiguous(), image_sizes)\n",
    "    \n",
    "class rpn_wrapper(nn.Module):\n",
    "    def __init__(self, rpn, size_divisibility):\n",
    "        super().__init__()\n",
    "        self.rpn = rpn\n",
    "        self.size_divisibility = size_divisibility\n",
    "        self.fp_keys = ['p2', 'p3', 'p4', 'p5', 'p6']\n",
    "        \n",
    "    def forward(self, x, fp_values):\n",
    "        images = ImageList.from_tensors([x], self.size_divisibility)\n",
    "        features = {k:v for (k,v) in zip(self.fp_keys, fp_values)}\n",
    "        proposals, losses = self.rpn(images, features, None)\n",
    "        proposals = proposals[0]._fields['proposal_boxes'].tensor\n",
    "        return proposals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 695.4552,  166.5025,  972.2765,  706.4489],\n",
       "        [ 265.3167,  125.6099,  600.0546,  722.2170],\n",
       "        [ 519.1044,   42.9846,  788.3871,  554.9060],\n",
       "        ...,\n",
       "        [ 557.5628,  435.4629,  587.3651,  535.2599],\n",
       "        [1325.5300,  332.0747, 1333.0000,  501.1771],\n",
       "        [  15.2157,  481.1315,   63.8876,  623.2001]], device='cuda:0')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "images = [x[\"image\"].to('cuda:0') for x in inpput]\n",
    "images = [model.normalizer(x) for x in images]\n",
    "\n",
    "rpn = model._modules['proposal_generator'].eval()\n",
    "rpn_wrap = rpn_wrapper(rpn, backbone.size_divisibility).eval()\n",
    "\n",
    "image = images[0]\n",
    "fp = backbone(backbone_example.tensor.to('cuda:0'))\n",
    "fp_values = tuple(fp.values())\n",
    "rpn_wrap(image, fp_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel_launcher.py:46: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel_launcher.py:47: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/anchor_generator.py:179: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  num_images = len(features[0])\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/structures/boxes.py:91: TracerWarning: torch.as_tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
      "  tensor = torch.as_tensor(tensor, dtype=torch.float32, device=device)\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/structures/boxes.py:94: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  assert tensor.dim() == 2 and tensor.size(-1) == 4, tensor.size()\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/box_regression.py:79: TracerWarning: Converting a tensor to a Python number might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  assert torch.isfinite(deltas).all().item(), \"Box regression deltas become infinite or NaN!\"\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py:103: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  num_proposals_i = min(pre_nms_topk, Hi_Wi_A)\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py:108: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  topk_scores_i = logits_i[batch_idx, :num_proposals_i]\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py:109: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  topk_idx = idx[batch_idx, :num_proposals_i]\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/structures/boxes.py:129: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  assert torch.isfinite(self.tensor).all()\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py:133: TracerWarning: Converting a tensor to a Python number might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  if keep.sum().item() != len(boxes):\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py:133: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  if keep.sum().item() != len(boxes):\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/layers/nms.py:13: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  assert boxes.shape[-1] == 4\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/layers/nms.py:16: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  if len(boxes) < 40000:\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/structures/instances.py:67: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  data_len = len(value)\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/structures/instances.py:131: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
      "  return len(v)\n"
     ]
    },
    {
     "ename": "TracingCheckError",
     "evalue": "Tracing failed sanity checks!\nEncountered an exception while running the trace with test inputs.\nException:\n\tDimension out of range (expected to be in range of [-1, 0], but got 1)\n\tThe above operation failed in interpreter, with the following stack trace:\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py(406): predict_proposals\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn.py(172): forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(525): _slow_forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(539): __call__\n\t<ipython-input-7-48f4e0422e9d>(80): forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(525): _slow_forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(539): __call__\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py(1020): trace_module\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py(880): trace\n\t<ipython-input-9-5f8c1d8e78f2>(6): <module>\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3326): run_code\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3249): run_ast_nodes\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3058): run_cell_async\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/async_helpers.py(68): _pseudo_sync_runner\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(2881): _run_cell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(2855): run_cell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/zmqshell.py(536): run_cell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/ipkernel.py(294): do_execute\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(542): execute_request\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(272): dispatch_shell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(365): process_one\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(748): run\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(787): inner\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/ioloop.py(743): _run_callback\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/ioloop.py(690): <lambda>\n\t/root/anaconda2/envs/python3/lib/python3.7/asyncio/events.py(88): _run\n\t/root/anaconda2/envs/python3/lib/python3.7/asyncio/base_events.py(1771): _run_once\n\t/root/anaconda2/envs/python3/lib/python3.7/asyncio/base_events.py(534): run_forever\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/platform/asyncio.py(148): start\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelapp.py(563): start\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/traitlets/config/application.py(664): launch_instance\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel_launcher.py(16): <module>\n\t/root/anaconda2/envs/python3/lib/python3.7/runpy.py(85): _run_code\n\t/root/anaconda2/envs/python3/lib/python3.7/runpy.py(193): _run_module_as_main\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py\u001b[0m in \u001b[0;36mrun_mod_and_filter_tensor_outputs\u001b[0;34m(mod, inputs, running_what)\u001b[0m\n\u001b[1;32m    637\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 638\u001b[0;31m                 \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwrap_retval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0m_clone_inputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    639\u001b[0m                 \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mout\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mout\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mouts\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mRuntimeError\u001b[0m: Dimension out of range (expected to be in range of [-1, 0], but got 1)\nThe above operation failed in interpreter, with the following stack trace:\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py(406): predict_proposals\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn.py(172): forward\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(525): _slow_forward\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(539): __call__\n<ipython-input-7-48f4e0422e9d>(80): forward\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(525): _slow_forward\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(539): __call__\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py(1020): trace_module\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py(880): trace\n<ipython-input-9-5f8c1d8e78f2>(6): <module>\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3326): run_code\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3249): run_ast_nodes\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3058): run_cell_async\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/async_helpers.py(68): _pseudo_sync_runner\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(2881): _run_cell\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(2855): run_cell\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/zmqshell.py(536): run_cell\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/ipkernel.py(294): do_execute\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(542): execute_request\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(272): dispatch_shell\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(365): process_one\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(748): run\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(787): inner\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/ioloop.py(743): _run_callback\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/ioloop.py(690): <lambda>\n/root/anaconda2/envs/python3/lib/python3.7/asyncio/events.py(88): _run\n/root/anaconda2/envs/python3/lib/python3.7/asyncio/base_events.py(1771): _run_once\n/root/anaconda2/envs/python3/lib/python3.7/asyncio/base_events.py(534): run_forever\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/platform/asyncio.py(148): start\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelapp.py(563): start\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/traitlets/config/application.py(664): launch_instance\n/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel_launcher.py(16): <module>\n/root/anaconda2/envs/python3/lib/python3.7/runpy.py(85): _run_code\n/root/anaconda2/envs/python3/lib/python3.7/runpy.py(193): _run_module_as_main\n",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mTracingCheckError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-5f8c1d8e78f2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mexample\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfp_values\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mtraced_script_module\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrpn_wrap\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexample\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py\u001b[0m in \u001b[0;36mtrace\u001b[0;34m(func, example_inputs, optimize, check_trace, check_inputs, check_tolerance, _force_outplace, _module_class, _compilation_unit)\u001b[0m\n\u001b[1;32m    878\u001b[0m         return trace_module(func, {'forward': example_inputs}, None,\n\u001b[1;32m    879\u001b[0m                             \u001b[0mcheck_trace\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwrap_check_inputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcheck_inputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 880\u001b[0;31m                             check_tolerance, _force_outplace, _module_class)\n\u001b[0m\u001b[1;32m    881\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    882\u001b[0m     if (hasattr(func, '__self__') and isinstance(func.__self__, torch.nn.Module) and\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py\u001b[0m in \u001b[0;36mtrace_module\u001b[0;34m(mod, inputs, optimize, check_trace, check_inputs, check_tolerance, _force_outplace, _module_class, _compilation_unit)\u001b[0m\n\u001b[1;32m   1028\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1029\u001b[0m                 _check_trace([inputs], func, check_trace_method,\n\u001b[0;32m-> 1030\u001b[0;31m                              check_tolerance, _force_outplace, True, _module_class)\n\u001b[0m\u001b[1;32m   1031\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1032\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/torch/autograd/grad_mode.py\u001b[0m in \u001b[0;36mdecorate_no_grad\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     47\u001b[0m         \u001b[0;32mdef\u001b[0m \u001b[0mdecorate_no_grad\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     48\u001b[0m             \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 49\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     50\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mdecorate_no_grad\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     51\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py\u001b[0m in \u001b[0;36m_check_trace\u001b[0;34m(check_inputs, func, traced_func, check_tolerance, force_outplace, is_trace_module, _module_class)\u001b[0m\n\u001b[1;32m    674\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mall_ok\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    675\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 676\u001b[0;31m         \u001b[0mtraced_outs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrun_mod_and_filter_tensor_outputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtraced_func\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'trace'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    677\u001b[0m         \u001b[0mfn_outs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrun_mod_and_filter_tensor_outputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Python function'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    678\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mcompare_outputs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtraced_outs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfn_outs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Python function'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py\u001b[0m in \u001b[0;36mrun_mod_and_filter_tensor_outputs\u001b[0;34m(mod, inputs, running_what)\u001b[0m\n\u001b[1;32m    642\u001b[0m                 raise TracingCheckError(*graph_diagnostic_info(),\n\u001b[1;32m    643\u001b[0m                                         \u001b[0mextra_msg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Encountered an exception while running the '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mrunning_what\u001b[0m \u001b[0;34m+\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 644\u001b[0;31m                                                   ' with test inputs.\\nException:\\n' + indent(str(e)))\n\u001b[0m\u001b[1;32m    645\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    646\u001b[0m         \u001b[0mhas_warned\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTracingCheckError\u001b[0m: Tracing failed sanity checks!\nEncountered an exception while running the trace with test inputs.\nException:\n\tDimension out of range (expected to be in range of [-1, 0], but got 1)\n\tThe above operation failed in interpreter, with the following stack trace:\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn_outputs.py(406): predict_proposals\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/detectron2/modeling/proposal_generator/rpn.py(172): forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(525): _slow_forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(539): __call__\n\t<ipython-input-7-48f4e0422e9d>(80): forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(525): _slow_forward\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/nn/modules/module.py(539): __call__\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py(1020): trace_module\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/torch/jit/__init__.py(880): trace\n\t<ipython-input-9-5f8c1d8e78f2>(6): <module>\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3326): run_code\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3249): run_ast_nodes\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(3058): run_cell_async\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/async_helpers.py(68): _pseudo_sync_runner\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(2881): _run_cell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/IPython/core/interactiveshell.py(2855): run_cell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/zmqshell.py(536): run_cell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/ipkernel.py(294): do_execute\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(542): execute_request\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(272): dispatch_shell\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(209): wrapper\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelbase.py(365): process_one\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(748): run\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/gen.py(787): inner\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/ioloop.py(743): _run_callback\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/ioloop.py(690): <lambda>\n\t/root/anaconda2/envs/python3/lib/python3.7/asyncio/events.py(88): _run\n\t/root/anaconda2/envs/python3/lib/python3.7/asyncio/base_events.py(1771): _run_once\n\t/root/anaconda2/envs/python3/lib/python3.7/asyncio/base_events.py(534): run_forever\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/tornado/platform/asyncio.py(148): start\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel/kernelapp.py(563): start\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/traitlets/config/application.py(664): launch_instance\n\t/root/anaconda2/envs/python3/lib/python3.7/site-packages/ipykernel_launcher.py(16): <module>\n\t/root/anaconda2/envs/python3/lib/python3.7/runpy.py(85): _run_code\n\t/root/anaconda2/envs/python3/lib/python3.7/runpy.py(193): _run_module_as_main\n"
     ]
    }
   ],
   "source": [
    "image = images[0]\n",
    "fp = backbone(backbone_example.tensor.to('cuda:0'))\n",
    "fp_values = tuple(fp.values())\n",
    "\n",
    "example = (image, fp_values, )\n",
    "traced_script_module = torch.jit.trace(rpn_wrap, example)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Tensorpack\n",
    "```git clone tensorpack```\n",
    "\n",
    "Follow:\n",
    "https://github.com/tensorpack/tensorpack/tree/master/examples/FasterRCNN\n",
    "\n",
    "Creates .pb tf graph\n",
    "\n",
    "NOTE: coco 2017 dataset is needed"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. FastRCNN to ONNX\n",
    "\n",
    "NOTE: pytorch-nightly (2019oct29) is needed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchvision\n",
    "\n",
    "model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\n",
    "model.eval()\n",
    "x = [torch.rand(3, 300, 400)]\n",
    "predictions = model(x)\n",
    "\n",
    "torch.onnx.export(model, (x,), './fasterrcnn.onnx', do_constant_folding=True, opset_version=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Then try to infer using Caffe2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import onnx\n",
    "\n",
    "# Load the ONNX model\n",
    "model = onnx.load(\"./fasterrcnn.onnx\")\n",
    "\n",
    "onnx.checker.check_model(model)\n",
    "\n",
    "onnx.helper.printable_graph(model.graph)\n",
    "\n",
    "import caffe2.python.onnx.backend as backend\n",
    "import numpy as np\n",
    "\n",
    "backend._known_opset_version = 10\n",
    "rep = backend.prepare(model, device=\"CUDA:0\")\n",
    "outputs = rep.run(np.random.rand(3, 300, 400).astype(np.float32))\n",
    "print(outputs[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. ONNX -> TF\n",
    "\n",
    "```pip install onnx-tf```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/ceil.py:10: The name tf.ceil is deprecated. Please use tf.math.ceil instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/depth_to_space.py:12: The name tf.depth_to_space is deprecated. Please use tf.compat.v1.depth_to_space instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/erf.py:9: The name tf.erf is deprecated. Please use tf.math.erf instead.\n",
      "\n",
      "WARNING:tensorflow:\n",
      "The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
      "For more information, please see:\n",
      "  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
      "  * https://github.com/tensorflow/addons\n",
      "  * https://github.com/tensorflow/io (for I/O related ops)\n",
      "If you depend on functionality not listed there, please file an issue.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/common/__init__.py:87: UserWarning: onnx_tf.common.get_outputs_names is deprecated. It will be removed in future release. Use TensorflowGraph.get_outputs_names instead.\n",
      "  warnings.warn(message)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/is_nan.py:9: The name tf.is_nan is deprecated. Please use tf.math.is_nan instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/log.py:10: The name tf.log is deprecated. Please use tf.math.log instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/random_normal.py:9: The name tf.random_normal is deprecated. Please use tf.random.normal instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/random_uniform.py:9: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/reciprocal.py:10: The name tf.reciprocal is deprecated. Please use tf.math.reciprocal instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/space_to_depth.py:12: The name tf.space_to_depth is deprecated. Please use tf.compat.v1.space_to_depth instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/upsample.py:13: The name tf.image.resize_images is deprecated. Please use tf.image.resize instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/xor.py:10: The name tf.logical_xor is deprecated. Please use tf.math.logical_xor instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/common/handler_helper.py:37: UserWarning: Unknown op ConstantFill in domain `ai.onnx`.\n",
      "  handler.ONNX_OP, handler.DOMAIN or \"ai.onnx\"))\n",
      "/root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/common/handler_helper.py:37: UserWarning: Unknown op ImageScaler in domain `ai.onnx`.\n",
      "  handler.ONNX_OP, handler.DOMAIN or \"ai.onnx\"))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/backend.py:109: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
      "\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend_handler.py:182: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Deprecated in favor of operator or tf.math.divide.\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/slice.py:77: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
      "WARNING:tensorflow:From /root/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/handlers/backend/slice.py:88: sparse_to_dense (from tensorflow.python.ops.sparse_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Create a `tf.sparse.SparseTensor` and use `tf.sparse.to_dense` instead.\n"
     ]
    },
    {
     "ename": "NotImplementedError",
     "evalue": "Resize is not implemented.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNotImplementedError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-2c1ed37c0f75>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0monnx_model\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0monnx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"./fasterrcnn.onnx\"\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m# load onnx model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mtf_rep\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprepare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0monnx_model\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m# prepare tf representation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m \u001b[0mtf_rep\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexport_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"./fasterrcnn.pb\"\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m# export the model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/backend.py\u001b[0m in \u001b[0;36mprepare\u001b[0;34m(cls, model, device, strict, **kwargs)\u001b[0m\n\u001b[1;32m     53\u001b[0m     \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTensorflowBackend\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprepare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdevice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0monnx_model_to_tensorflow_rep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstrict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     56\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     57\u001b[0m   \u001b[0;34m@\u001b[0m\u001b[0mclassmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/backend.py\u001b[0m in \u001b[0;36monnx_model_to_tensorflow_rep\u001b[0;34m(cls, model, strict)\u001b[0m\n\u001b[1;32m     73\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     74\u001b[0m       \u001b[0mopset_import\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopset_import\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 75\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_onnx_graph_to_tensorflow_rep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgraph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mopset_import\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstrict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     76\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     77\u001b[0m   \u001b[0;34m@\u001b[0m\u001b[0mclassmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/backend.py\u001b[0m in \u001b[0;36m_onnx_graph_to_tensorflow_rep\u001b[0;34m(cls, graph_def, opset, strict)\u001b[0m\n\u001b[1;32m    127\u001b[0m         \u001b[0monnx_node\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mOnnxNode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    128\u001b[0m         output_ops = cls._onnx_node_to_tensorflow_op(\n\u001b[0;32m--> 129\u001b[0;31m             onnx_node, tensor_dict, handlers, opset=opset, strict=strict)\n\u001b[0m\u001b[1;32m    130\u001b[0m         \u001b[0mcurr_node_output_map\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0monnx_node\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput_ops\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    131\u001b[0m         \u001b[0mtensor_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurr_node_output_map\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/backend.py\u001b[0m in \u001b[0;36m_onnx_node_to_tensorflow_op\u001b[0;34m(cls, node, tensor_dict, handlers, opset, strict)\u001b[0m\n\u001b[1;32m    224\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0mhandler\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhandle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtensor_dict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtensor_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstrict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstrict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    225\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 226\u001b[0;31m       \u001b[0mexception\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOP_UNIMPLEMENTED_EXCEPT\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mop_type\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    227\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    228\u001b[0m   \u001b[0;34m@\u001b[0m\u001b[0mclassmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/common/exception.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, op, version, domain)\u001b[0m\n\u001b[1;32m     29\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mIGNORE_UNIMPLEMENTED\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     30\u001b[0m       \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_func\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwarnings\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m     \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mOpUnimplementedException\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mversion\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdomain\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     33\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0mget_message\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mversion\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdomain\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda2/envs/python3/lib/python3.7/site-packages/onnx_tf/common/exception.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m     11\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0minspect\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misclass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_func\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0missubclass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_func\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m       \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_message\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     14\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_func\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m       \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_message\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNotImplementedError\u001b[0m: Resize is not implemented."
     ]
    }
   ],
   "source": [
    "import onnx\n",
    "\n",
    "from onnx_tf.backend import prepare\n",
    "\n",
    "onnx_model = onnx.load(\"./fasterrcnn.onnx\")  # load onnx model\n",
    "tf_rep = prepare(onnx_model)  # prepare tf representation\n",
    "tf_rep.export_graph(\"./fasterrcnn.pb\")  # export the model"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
